﻿using ServerCrafter.ClassLibrary;
using ServerCrafter.ClassLibrary.ViewModels;
using System.Windows;
using System.Windows.Input;
using Telerik.Windows.Documents.Model;

namespace ServerCrafter.WPF
{
    /// <summary>
    /// Controls that are in tab #1 "General".
    /// </summary>
    public partial class MainWindow
    {
        #region Server Controls

        /// <summary>
        /// Handles the Click event of the btnClearOutput control. Clears the output text box.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnClearOutput_Click(object sender, RoutedEventArgs e)
        {
            txtboxOutput.Document = new RadDocument();
        }

        /// <summary>
        /// Handles the Click event of the toggleAutoScroll control. Toggles whether the text box will automatically scroll to the bottom.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void toggleAutoScroll_Click(object sender, RoutedEventArgs e)
        {
            if (toggleAutoScroll.IsChecked != null && (bool)toggleAutoScroll.IsChecked)
                ServerOutputHandler.IsAutoScrollChecked = true;
            else
                ServerOutputHandler.IsAutoScrollChecked = false;
        }

        /// <summary>
        /// Handles the Click event of the toggleSayCmd control. Toggles whether "say " is prefixes to each command.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void toggleSayCmd_Click(object sender, RoutedEventArgs e)
        {
            if (toggleSayCmd.IsChecked != null && (bool)toggleSayCmd.IsChecked)
                Server.Options.IsSayChecked = true;
            else
                Server.Options.IsSayChecked = false;
        }

        /// <summary>
        /// Handles the Click event of the btnExecuteCmd control. Executes the specified server command.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnExecuteCmd_Click(object sender, RoutedEventArgs e)
        {
            var selectedItem = txtboxCommand.SelectedItem;
            var selectedCommand = selectedItem as Command;
            if (selectedCommand != null)
            {
                Server.Controls.Give(selectedCommand.Text + " ");
            }
            Server.Controls.Give(txtboxCommand.SearchText);

            txtboxCommand.SearchText = null;
            txtboxCommand.SelectedItems = null;
        }

        /// <summary>
        /// Handles the KeyDown event of the txtboxCommand control. Executes the specified server command.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
        private void txtboxCommand_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return) return;

            var selectedItem = txtboxCommand.SelectedItem;
            var selectedCommand = selectedItem as Command;
            if (selectedCommand != null)
            {
                Server.Controls.Give(selectedCommand.Text + " ");
            }
            Server.Controls.Give(txtboxCommand.SearchText);

            txtboxCommand.SearchText = null;
            txtboxCommand.SelectedItems = null;
        }

        #endregion Server Controls

        #region Server Control Buttons

        /// <summary>
        /// Handles the Click event of the btnStartServer control. Starts the server.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnStartServer_Click(object sender, RoutedEventArgs e)
        {
            Server.Controls.Start();
        }

        /// <summary>
        /// Handles the Click event of the btnRestartServer control. Restarts the server.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnRestartServer_Click(object sender, RoutedEventArgs e)
        {
            Server.Controls.Restart();
        }

        /// <summary>
        /// Handles the Click event of the btnReloadPlugins control. Reloads the server's plugins.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnReloadPlugins_Click(object sender, RoutedEventArgs e)
        {
            Server.Controls.Reload();
        }

        /// <summary>
        /// Handles the Click event of the btnStopServer control. Stops the server.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnStopServer_Click(object sender, RoutedEventArgs e)
        {
            Server.Controls.Stop();
        }

        /// <summary>
        /// Handles the Click event of the btnKillServer control. Kills the server
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnKillServer_Click(object sender, RoutedEventArgs e)
        {
            Server.Controls.Kill();
        }

        /// <summary>
        /// Handles the Click event of the btnSaveAll control. Saves the server.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSaveAll_Click(object sender, RoutedEventArgs e)
        {
            Server.Controls.Save();
        }

        #endregion Server Control Buttons
    }
}